<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Controller;
use think\Request;

class ProjectOrder extends Backend
{
    protected $navTitle = "project";

    public function __construct()
    {
        parent::__construct();

        $this->ProjectModel = model('Project.Project');
        $this->ProjectOrderModel = model('Project.ProjectOrder');
    }

    public function index()
    {
        $project_order_list = $this->ProjectOrderModel->with(['address', 'user', 'project'])->paginate(10);

        $project = $this->ProjectModel->select();

        $status = [
            '0' => '未接单',
            '1' => '待付款',
            '2' => '已付款，可申请撤销',
            '3' => '已评价',
            '-1' => '已退货',
            '-2' => '管理员审核',
            '-3' => '拒绝撤销',
        ];

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'project_order_list' => $project_order_list,
            'project' => $project,
            'status' => $status,

        ]);
    }

    //  TODO 根据id查询对应的项目订单
    public function query()
    {
        $pid = $this->request->param();

        // halt($pid);

        $project_order = $this->ProjectOrderModel->with(['user', 'address', 'project'])->find($pid);

        if (!$project_order) $this->error('项目订单不存在');

        $project = $this->ProjectModel->select();

        $status = [
            '0' => '未接单',
            '1' => '待付款',
            '2' => '已付款，可申请撤销',
            '3' => '已评价',
            '-1' => '已退货',
            '-2' => '管理员审核',
            '-3' => '拒绝撤销',
        ];

        $data = [
            'project_order' => $project_order,
            'project' => $project,
            'status' => $status,

        ];

        $this->success('查询成功', '', $data);
    }

    // TODO 查询地区数据
    public function area()
    {
        if ($this->request->isAjax()) {
            // TODO 接收区划码
            $code = $this->request->param('code', '');
            // TODO 根据区划码查询省份下的市
            $cityList = model('Region')->where(['parentid' => $code])->select();

            if (!$cityList) {
                $this->error('所选地区不存在');
            }

            $this->success('查询成功', '', $cityList);
        }
    }

    public function edit()
    {
        $formData = $this->request->param();

        halt($formData);

        $project_order = $this->ProjectOrderModel->find($formData['id']);

        if(!$project_order) $this->error('预约订单不存在');

        $data = [];
    }

    // TODO 回收站
    public function appman()
    {
        $ProjectOrderList = $this->ProjectOrderModel->onlyTrashed()->with(['address', 'user', 'project'])->paginate(10);

        if ($this->request->isAjax()) {
            $formData = $this->request->param();

            $action = $this->request->param('action');

            if ($action == 'return') {
                $pid = $this->request->param('pid', '');

                $project = $this->ProjectOrderModel->onlyTrashed()->find($pid);

                if (!$project) $this->error('项目订单不存在');

                $project->restore() === FALSE ? $this->error('项目订单还原失败') : $this->success('项目订单还原成功');
            }

            if ($action == 'returns') {
                // TODO 转存idArr
                $idArr = $formData['idArr'] ? $formData['idArr'] : [];

                $result = $this->ProjectOrderModel->onlyTrashed()->whereIn('id', $idArr)->update(['deletetime' => null]);

                $result === FALSE ? $this->error('项目订单还原失败,' . $this->ProjectOrderModel->getError()) : $this->success('项目订单还原成功');
            }

            if ($action == 'del') {
                $pid = $this->request->param('pid', '');

                $project = $this->ProjectOrderModel->onlyTrashed()->find($pid);

                if (!$project) $this->error('项目订单不存在');

                $result = $this->ProjectOrderModel->destroy($pid, true);

                $result === FALSE ? $this->error('项目订单删除失败,' . $this->ProjectOrderModel->getError()) : $this->success('项目订单删除成功');
            }

            if ($action == 'dels') {
                // TODO 接收所有数据
                $idArr = $formData['idArr'] ? $formData['idArr'] : [];

                $result = $this->ProjectOrderModel->destroy($idArr, true);

                $result === FALSE ? $this->error('项目订单删除失败' . $this->ProjectOrderModel->getError()) : $this->success('项目订单删除成功');
            }
        }

        return $this->fetch('', [
            'navTitle' => $this->navTitle,
            'ProjectOrderList' => $ProjectOrderList
        ]);
    }

    // TODO 软删除
    public function delete()
    {
        if ($this->request->isAjax()) {
            $action = $this->request->param('action', '');

            if ($action == 'del') {
                $pid = $this->request->param('pid', '');

                $ProjectOrder = $this->ProjectOrderModel->find($pid);

                if (!$ProjectOrder) $this->error('项目订单不存在');

                $result = $this->ProjectOrderModel->destroy($pid);

                $result === FALSE ? $this->error('项目订单移入回收站失败') : $this->success('项目订单移入回收站成功', url('admin/order_product/index'));
            }

            if ($action == 'dels') {
                // TODO 接收所有数据
                $formData = $this->request->param();
                // TODO 转存idArr
                $idArr = $formData['idArr'];

                $result = $this->ProjectOrderModel->destroy($idArr);

                $result === FALSE ? $this->error('项目订单移入回收站失败') : $this->success('项目订单移入回收站成功');
            }
        }
    }
}
